#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

typedef struct
{
    char name[6];
    int age;
} people;

int main(int argc, char* argv[])
{
    int fd, i;

    people *p_map;
    people *t;
    char temp;
    fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 00777);
    lseek(fd, sizeof(people) * 5 - 1, SEEK_SET);
    write(fd, "", 1);

    p_map = (people*) mmap(NULL, sizeof(people) * 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (p_map == (void*) -1)
    {
        fprintf(stderr, "mmap: %s\n", strerror(errno));
    }
    close(fd);

    temp = 'A';
    for (i = 0; i < 10; i++)
    {
        t = p_map + i;
        temp = temp + 1;
        t->name[1] = '\0';
        memcpy(&(t->name), &temp, 1);
        t->age = 30 + i;
    }

    printf("initialized\n");
    sleep(15);
    munmap(p_map, sizeof(people) * 10);
    printf("UMA OK.\n");
    return 0;
}
